local skynet = require "skynet.manager"
local httpc = require "http.httpc"
local cjson = require "cjson"
local hosts = require "hosts"
local constant = require "constant"

-- httpc.timeout = 10 * 100 --ten seconds.
-- httpc.dns()

-- local ACCOUNT_WEBSERVER_URL = hosts.resolve(skynet.getenv("ACCOUNT_WEBSERVER_USERINFO_URL") or "127.0.0.1:6668")

local handlers = {}

-- function handlers.common_post(exchange_url,url,headers,data)
--     print("common_post,exchange_url",exchange_url,"url",url)
--     local status,body = httpc.request('POST',exchange_url,url,nil,headers,data)
--     if status ~= 200 then
--         errlog('failed to common_post',status,tostring_r(body))
--         return status,nil
--     end

--     if not body then
--         errlog('common_post invalid response',body)
--         return -100,nil
--     end

--     return status,body
-- end

local function get_user_info(uid)
    local body = cjson.encode({uid = uid})
    local status, body = skynet.call('.web_proxy','lua','web_post',constant.GET_USER_INFO,body)
    -- local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
    --     "/starry/user/get_user_info",nil,nil,body)
    if status ~= 200 then
        errlog(uid,'failed to get_user_info',status)
        return {}
    end
    
    if not body then
        errlog(uid,'invalid response',body)
        return {}
    end

    return cjson.decode(body)
end

function handlers.get_user_info(uid)
    local ok,ret = xpcall(get_user_info,debug.traceback,uid)
    if not ok then
        errlog(uid,ret)
        return {}
    end
    return ret
end

-- function handlers.bind_inviter(uid,inviter_code)
--     local body = cjson.encode({uid = uid,inviter_code=inviter_code})
--     local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
--         "/starry/user/bind_inviter",nil,nil,body)
--     if status ~= 200 then
--         errlog(uid,'failed to get_user_info',status)
--         return {}
--     end
    
--     if not body then
--         errlog(uid,'invalid response',body)
--         return {}
--     end

--     return cjson.decode(body)
-- end

-- function handlers.get_inviter(uid,inviter_code)
--     local body = cjson.encode({uid = uid,inviter_code=inviter_code})
--     local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
--         "/starry/user/get_inviter",nil,nil,body)
--     if status ~= 200 then
--         errlog(uid,'failed to get_user_info',status)
--         return {}
--     end
    
--     if not body then
--         errlog(uid,'invalid response',body)
--         return {}
--     end

--     return cjson.decode(body)
-- end

-- function handlers.get_user_register_info(uid)
--     local data = cjson.encode({uid = uid})
--     local status, body = skynet.call('.web_proxy','lua','web_post',constant.IS_TOURIST_URL,data)
--     -- local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
--     --     "/starry/user/is_tourist",nil,nil,body)

--     if status ~= 200 then
--         return {code = -1}
--     end

--     local result = cjson.decode(body)
--     if result.code < 0 then
--         return {code = -1}
--     end

--     return result
-- end

-- function handlers.is_player_exist(uid)
--     print("is_player_exist==========",uid)
--     local body = cjson.encode({uid = uid})
--     local status, body = httpc.request('POST',ACCOUNT_WEBSERVER_URL, 
--         "/starry/user/is_tourist",nil,nil,body)

--     print("is_player_exist==========2222",status,tostring_r(body))
--     if status ~= 200 then
--         return false
--     end

--     local result = cjson.decode(body)
--     if result.code == -1 then
--         return false
--     end

--     return true
-- end

function handlers.finish_payment()

end

skynet.start(function()
	skynet.dispatch("lua",function(_,_,action, ...)
		local f = assert(handlers[action])
        skynet.retpack(f(...))
	end)
end)


